package com.selfdriving.wl.activity;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioButton;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.activity.AboutMeMyArticleDetails;
import com.selfdriving.adapter.FeatureDiaryAdapter;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.MyArticleDetails;
import com.selfdriving.bean.ScenicNotes;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.Utils;
import com.selfdriving.view.MoMoRefreshListView;
import com.selfdriving.view.MoMoRefreshListView.OnCancelListener;
import com.selfdriving.view.MoMoRefreshListView.OnRefreshListener;

public class PublicComment implements OnClickListener, OnRefreshListener,
		OnCancelListener
{
	/** 当前View **/
	View view;
	BaseApplication application;
	Context context;
	MoMoRefreshListView comment_list;
	MoMoRefreshListView features_list;
	MoMoRefreshListView strategy_list;

	ListView feature_list;
	ListView album_list;
	RadioButton featureComment;
	RadioButton featureDiary;
	RadioButton featureStrategy;
	private FeatureDiaryAdapter commentAdapter;
	private FeatureDiaryAdapter featureDiaryAdapter;
	private FeatureDiaryAdapter pictureAlbumAdapter;
	private List<Entity> comments = new ArrayList<Entity>();
	private List<Entity> features = new ArrayList<Entity>();
	private List<Entity> strategy = new ArrayList<Entity>();

	LayoutInflater inflater;

	private int flag = 1;

	private int page1 = 1;
	private int page2 = 1;
	private int page3 = 1;

	String city = "";
	String type = "0";

	public PublicComment(BaseApplication application, Context context)
	{
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.public_comment, null);
		this.application = application;
		this.context = context;
		initViews();
	}

	public void initViews()
	{
		comment_list = (MoMoRefreshListView) view
				.findViewById(R.id.comment_list);
		strategy_list = (MoMoRefreshListView) view
				.findViewById(R.id.comment_list);
		features_list = (MoMoRefreshListView) view
				.findViewById(R.id.comment_list);

		featureComment = (RadioButton) view.findViewById(R.id.tab_item_1);
		featureDiary = (RadioButton) view.findViewById(R.id.tab_item_2);
		featureStrategy = (RadioButton) view.findViewById(R.id.tab_item_3);
		featureComment.setOnClickListener(this);
		featureDiary.setOnClickListener(this);
		featureStrategy.setOnClickListener(this);

		commentAdapter = new FeatureDiaryAdapter(application, context,
				comments, 1);
		strategy_list.setOnRefreshListener(this);
		strategy_list.setOnCancelListener(this);

		features_list.setOnRefreshListener(this);
		features_list.setOnCancelListener(this);
		featureDiaryAdapter = new FeatureDiaryAdapter(application, context,
				features, 1);

		// feature_list.setAdapter(featureDiaryAdapter);

		pictureAlbumAdapter = new FeatureDiaryAdapter(application, context,
				strategy, 1);

		// album_list.setAdapter(pictureAlbumAdapter);

		comment_list.setOnRefreshListener(this);
		comment_list.setOnCancelListener(this);

		comment_list.setAdapter(commentAdapter);
		featureComment.setChecked(true);

		strategy_list.setOnItemClickListener(onItemClickListener);
		features_list.setOnItemClickListener(onItemClickListener);
	}

	OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3)
		{
			if (arg2 > 0)
			{
				ScenicNotes notes = null;
				if (flag == 1)
				{
					notes = (ScenicNotes) comments.get(arg2 - 1);
				}
				if (flag == 2)
				{
					notes = (ScenicNotes) features.get(arg2 - 1);
				} else if (flag == 3)
				{
					notes = (ScenicNotes) strategy.get(arg2 - 1);
				}
				post(notes.getId());
			}
		}
	};

	public View getView()
	{
		return view;
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.tab_item_1:
			page1 = 1;
			comment_list.setAdapter(commentAdapter);
			getDatas1(city, type);
			// feature_list.setVisibility(View.GONE);
			// album_list.setVisibility(View.GONE);
			featureComment.setChecked(true);
			featureDiary.setChecked(false);
			featureStrategy.setChecked(false);
			flag = 1;
			break;
		case R.id.tab_item_2:
			page2 = 1;
			features_list.setAdapter(featureDiaryAdapter);
			getDatas2();
			// feature_list.setVisibility(View.VISIBLE);
			// album_list.setVisibility(View.GONE);
			featureComment.setChecked(false);
			featureDiary.setChecked(true);
			featureStrategy.setChecked(false);
			flag = 2;
			break;
		case R.id.tab_item_3:
			page3 = 1;
			strategy_list.setAdapter(pictureAlbumAdapter);
			getDatas3();
			// feature_list.setVisibility(View.GONE);
			// album_list.setVisibility(View.VISIBLE);
			featureComment.setChecked(false);
			featureDiary.setChecked(false);
			featureStrategy.setChecked(true);
			flag = 3;
			break;
		default:
			break;
		}

	}

	public void getDatas1(String city, String type)
	{
		comments.clear();
		comment_list.mState = 2;
		comment_list.changeHeaderViewByState();
		AjaxParams params = new AjaxParams();
		params.put("f", "39");
		params.put("city", city);
		params.put("page", "" + page1);
		params.put("type", type);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.showShortToast(context, "获取景点信息失败");
				comment_list.onRefreshComplete();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				System.out.println(json);
				try
				{
					JSONArray array = new JSONArray(json);
					ScenicNotes scenic;
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject object = array.getJSONObject(i);
						String id = object.getString("id");
						String uname = object.getString("uname");
						String content = object.getString("content");
						String title = object.getString("title");
						String reviewSize = object.getString("reviewSize");
						String headImg = object.getString("headImg");
						String img = object.getString("img");
						String time = object.getString("time");
						scenic = new ScenicNotes(id, uname, content, title,
								reviewSize, headImg, img, time);
						comments.add(scenic);
					}
					page2++;
					commentAdapter.notifyDataSetChanged();
					comment_list.onRefreshComplete();
				} catch (JSONException e)
				{
					e.printStackTrace();
					comment_list.onRefreshComplete();
				}
			}
		});
	}

	public void getDatas2()
	{
		features.clear();
		features_list.mState = 2;
		features_list.changeHeaderViewByState();
		AjaxParams params = new AjaxParams();
		params.put("f", "40");
		params.put("page", "" + page2);
		params.put("type", "0");
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.showShortToast(context, "获取景点信息失败");
				features_list.onRefreshComplete();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				System.out.println(json);
				try
				{
					JSONArray array = new JSONArray(json);
					ScenicNotes scenic;
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject object = array.getJSONObject(i);
						String id = object.getString("id");
						String uname = object.getString("uname");
						String content = object.getString("content");
						String title = object.getString("title");
						String reviewSize = object.getString("reviewSize");
						String headImg = object.getString("headImg");
						String img = object.getString("img");
						String time = object.getString("time");
						scenic = new ScenicNotes(id, uname, content, title,
								reviewSize, headImg, img, time);
						features.add(scenic);
					}
					page2++;
					featureDiaryAdapter.notifyDataSetChanged();
					features_list.onRefreshComplete();
				} catch (JSONException e)
				{
					e.printStackTrace();
					features_list.onRefreshComplete();
				}
			}
		});
	}

	public void getDatas3()
	{
		strategy.clear();
		strategy_list.mState = 2;
		strategy_list.changeHeaderViewByState();
		AjaxParams params = new AjaxParams();
		params.put("f", "42");
		params.put("page", "" + page3);
		params.put("type", "0");
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.showShortToast(context, "获取景点信息失败");
				strategy_list.onRefreshComplete();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				System.out.println(json);
				try
				{
					JSONArray array = new JSONArray(json);
					ScenicNotes scenic;
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject object = array.getJSONObject(i);
						String id = object.getString("id");
						String uname = object.getString("uname");
						String content = object.getString("content");
						String title = object.getString("title");
						String reviewSize = object.getString("reviewSize");
						String headImg = object.getString("headImg");
						String img = object.getString("img");
						String time = object.getString("time");
						scenic = new ScenicNotes(id, uname, content, title,
								reviewSize, headImg, img, time);
						strategy.add(scenic);
					}
					page3++;
					pictureAlbumAdapter.notifyDataSetChanged();
					strategy_list.onRefreshComplete();
				} catch (JSONException e)
				{
					e.printStackTrace();
					strategy_list.onRefreshComplete();
				}
			}
		});
	}

	public void post(String id)
	{
		Utils.showLoadingDialog(context, "数据加载中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "19");
		params.put("id", id);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				try
				{
					JSONObject object = new JSONObject(json);
					String id = object.getString("id");
					String content = object.getString("content");
					String title = object.getString("title");
					String uid = object.getString("uid");
					String uname = object.getString("uname");
					String times = object.getString("times");
					String laud = object.getString("laud");
					String marrow = object.getString("marrow");
					String clicks = object.getString("clicks");
					String img0 = null;
					String img1 = null;
					String img2 = null;
					if (object.has("img0"))
					{
						img0 = object.getString("img0");
					}
					if (object.has("img1"))
					{
						img1 = object.getString("img1");
					}
					if (object.has("img2"))
					{
						img2 = object.getString("img2");
					}
					application.myArticleDetails = new MyArticleDetails(id,
							content, title, uid, uname, times, laud, marrow,
							clicks, img0, img1, img2);
					Utils.dismissLoadingDialog();
					Intent intent = new Intent(context,
							AboutMeMyArticleDetails.class);
					context.startActivity(intent);
				} catch (JSONException e)
				{
					Utils.dismissLoadingDialog();
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void onCancel()
	{
		comment_list.onRefreshComplete();
	}

	@Override
	public void onRefresh()
	{
		if (flag == 1)
		{
			page1 = 1;
			getDatas1(city, type);
		} else if (flag == 2)
		{
			page2 = 1;
			getDatas2();
		} else if (flag == 3)
		{
			page3 = 1;
			getDatas3();
		}
	}

	@Override
	public void onLoadMore()
	{

	}

}
